from django.contrib.auth.models import User, Group, Permission
from rest_framework.serializers import ModelSerializer, SlugRelatedField

from base.models import (
    Business,
    Environment,
    Application,
    ApplicationTag,
    Instances,
    Language,
    Resource,
)


class PermissionSerializer(ModelSerializer):
    content_type = SlugRelatedField(slug_field="name", read_only=True)

    class Meta:
        model = Permission
        fields = "__all__"


class GroupSerializer(ModelSerializer):
    class Meta:
        model = Group
        fields = ("id", "name")


class UserSerializer(ModelSerializer):
    groups = GroupSerializer(many=True)
    user_permissions = PermissionSerializer(many=True)

    class Meta:
        model = User
        fields = (
            "id",
            "last_login",
            "is_superuser",
            "username",
            "first_name",
            "last_name",
            "email",
            "is_staff",
            "is_active",
            "user_permissions",
            "groups",
        )


class BusinessSerializer(ModelSerializer):
    class Meta:
        model = Business
        fields = "__all__"


class EnvironmentSerializer(ModelSerializer):
    business = SlugRelatedField(slug_field="name", queryset=Business.objects.all())

    class Meta:
        model = Environment
        fields = (
            "id",
            "created_by",
            "created_at",
            "updated_by",
            "updated_at",
            "name",
            "instances",
            "business",
            "private_ip",
            "public_ip",
        )


class LanguageSerializer(ModelSerializer):
    class Meta:
        model = Language
        fields = (
            "name",
            "base_image",
            "jvm_opts",
            "run_base_path",
            "gc_opts",
            "metrics_port",
            "metrics_path",
        )


class ResourceSerializer(ModelSerializer):
    class Meta:
        model = Resource
        fields = (
            "name",
            "limit_cpu",
            "limit_memory",
            "request_cpu",
            "request_memory",
        )


class ApplicationSerializer(ModelSerializer):
    business = SlugRelatedField(slug_field="name", queryset=Business.objects.all())
    language = LanguageSerializer()
    resource = ResourceSerializer()

    class Meta:
        model = Application
        fields = (
            "id",
            "created_by",
            "created_at",
            "updated_by",
            "updated_at",
            "name",
            "desc",
            "language",
            "jvm_opts",
            "path",
            "health",
            "post_start",
            "pre_stop",
            "port",
            "image",
            "domain",
            "svc",
            "metrics",
            "initial_delay_seconds",
            "business",
            "resource",
        )


class ApplicationTagSerializer(ModelSerializer):
    application = SlugRelatedField(
        slug_field="name", queryset=Application.objects.all()
    )

    class Meta:
        model = ApplicationTag
        fields = (
            "id",
            "application",
            "tag",
        )


class InstancesSerializer(ModelSerializer):
    class Meta:
        model = Instances
        fields = "__all__"
